package com.xy.p2p.action.front;



import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Tyedtransferout;
import com.xy.p2p.service.TfinanceinfoService;
import com.xy.p2p.service.TtenderrecordService;
import com.xy.p2p.service.TyedtransferoutService;



@Controller
public class TyedtransferoutAction{
	private static final Logger log = LoggerFactory.getLogger(TyedtransferoutAction.class);
	public TyedtransferoutAction() {
	}
 
	
	@RequestMapping("/member/o_tyedtransferoutSave.jspx")
	public void save(long trid, double money, HttpServletRequest request, ModelMap model, HttpServletResponse response) {
		String msg = "";
		try {
			Tyedtransferout entity = new Tyedtransferout();
			Tmember tmember = (Tmember) request.getSession().getAttribute(TmemberAction.AUTH_KEY);
			entity.setTmember(tmember);
			entity.setToam(money);
			entity.setToamdate(new Date());
			entity.setStatus(2);
			entity.setTtenderrecord(ttenderrecordService.get(trid));
			tyedtransferoutService.save(entity);
			response.setContentType("text/Xml;charset=gbk");
			PrintWriter out = response.getWriter();
			msg="1";
			out.println(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 

	/**
	 * 余额贷转出申请表查询（分页）
	 * 
	 * @param tyedtransferout
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/member/v_tyedtransferoutList.jspx")
	public String listPage(Tyedtransferout tyedtransferout, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Tmember tmember = (Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY);
		tyedtransferout.setTmember(tmember);
		Pagination pagination = tyedtransferoutService.getTyedtransferoutByPage(toMap(tyedtransferout),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
        //用于导航栏
        model.addAttribute("dis", 9);
		return "/front/member/tyedtransferoutList.html";
	}

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tyedtransferout
	 * @return
	 */	private Map<String,Object> toMap(Tyedtransferout tyedtransferout){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tyedtransferout.getToid()!=null&&!tyedtransferout.getToid().equals("")){
			map.put("toid", tyedtransferout.getToid());
		}
		if(tyedtransferout.getTmember()!=null&&!tyedtransferout.getTmember().equals("")){
			map.put("Tmember", tyedtransferout.getTmember());
		}
		if(tyedtransferout.getToam()!=null&&!tyedtransferout.getToam().equals("")){
			map.put("toam", tyedtransferout.getToam());
		}
		if(tyedtransferout.getToamdate()!=null&&!tyedtransferout.getToamdate().equals("")){
			map.put("toamdate", tyedtransferout.getToamdate());
		}
		if(tyedtransferout.getTuser()!=null&&!tyedtransferout.getTuser().equals("")){
			map.put("Tuser", tyedtransferout.getTuser());
		}
		if(tyedtransferout.getRevieweddate()!=null&&!tyedtransferout.getRevieweddate().equals("")){
			map.put("revieweddate", tyedtransferout.getRevieweddate());
		}
		if(tyedtransferout.getOperator()!=null&&!tyedtransferout.getOperator().equals("")){
			map.put("operator", tyedtransferout.getOperator());
		}
		if(tyedtransferout.getOperatedate()!=null&&!tyedtransferout.getOperatedate().equals("")){
			map.put("operatedate", tyedtransferout.getOperatedate());
		}
		if(tyedtransferout.getStatus()!=null&&!tyedtransferout.getStatus().equals("")){
			map.put("status", tyedtransferout.getStatus());
		}
		if(tyedtransferout.getRemark()!=null&&!tyedtransferout.getRemark().equals("")){
			map.put("remark", tyedtransferout.getRemark());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TYEDTRANSFEROUT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tyedtransferout tyedtransferout, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TyedtransferoutService tyedtransferoutService;   
	@Resource
	private TtenderrecordService ttenderrecordService;
}